﻿using Sidvall.FileSystemManager.Net.Services.V1;
using System.Threading.Tasks;

namespace Sidvall.FileSystemManager
{
	public partial class SystemContext
	{
		#region Public Members

		#region UserConfiguration

		private UserConfiguration _UserConfiguration;
		public UserConfiguration UserConfiguration
		{
			get
			{
				return _UserConfiguration;
			}
			set
			{
				_UserConfiguration = GetValidConfiguration(value);
			}
		}

		#endregion
		#region Version

		private string _Version;
		public string Version
		{
			get
			{
				return _Version;
			}
			set
			{
				_Version = GetValidVersion(value);
			}
		}

		#endregion

		#region LoadUserConfigurationAsync

		public async Task<UserConfiguration> LoadUserConfigurationAsync()
		{
			string content;

			content = null;
			try
			{
				if (await Sidvall.SystemContext.Current.UserStorageManager.FileExistsAsync(ParameterManager.ConfigFilePath).ConfigureAwait(false))
					content = await Sidvall.SystemContext.Current.UserStorageManager.ReadAllTextAsync(ParameterManager.ConfigFilePath).ConfigureAwait(false);
			}
			catch (System.Exception)
			{
				content = null;
			}
			return LoadUserConfiguration(content);
		}

		#endregion
		#region LoadUserConfiguration

		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
		public UserConfiguration LoadUserConfiguration(string content)
		{
			UserConfiguration configuration;

			configuration = null;
			try
			{
				if (!string.IsNullOrWhiteSpace(content))
					configuration = Sidvall.Serialization.SerializerManager.Current.Deserialize<UserConfiguration>(content);
			}
			catch
			{
				configuration = null;
			}
			return LoadUserConfiguration(configuration);
		}
		public UserConfiguration LoadUserConfiguration(UserConfiguration configuration)
		{
			configuration = GetValidConfiguration(configuration);
			this.UserConfiguration = configuration;

			return this.UserConfiguration;
		}

		#endregion
		#region SaveUserConfigurationAsync

		public async Task<UserConfiguration> SaveUserConfigurationAsync(UserConfiguration configuration)
		{
			string content;

			configuration = GetValidConfiguration(configuration);
			content = Sidvall.Serialization.SerializerManager.Current.SerializeObject(configuration);
			await Sidvall.SystemContext.Current.UserStorageManager.WriteAllTextAsync(ParameterManager.ConfigFilePath, content).ConfigureAwait(false);
			this.UserConfiguration = configuration;

			return this.UserConfiguration;
		}

		#endregion

		#region LoadVersionAsync

		public async Task<string> LoadVersionAsync()
		{
			string version;

			version = null;
			try
			{
				if (await Sidvall.SystemContext.Current.UserStorageManager.FileExistsAsync(ParameterManager.VersionFilePath).ConfigureAwait(false))
					version = await Sidvall.SystemContext.Current.UserStorageManager.ReadAllTextAsync(ParameterManager.VersionFilePath).ConfigureAwait(false);
			}
			catch (System.Exception)
			{
				version = null;
			}
			this.Version = version;

			return this.Version;
		}

		#endregion
		#region SaveVersionAsync

		public async Task<string> SaveVersionAsync(string version)
		{
			version = GetValidVersion(version);
			await Sidvall.SystemContext.Current.UserStorageManager.WriteAllTextAsync(ParameterManager.VersionFilePath, version).ConfigureAwait(false);
			this.Version = version;

			return this.Version;
		}

		#endregion

		#endregion
		#region Private Members

		#region GetValidConfiguration

		private static UserConfiguration GetValidConfiguration(UserConfiguration item)
		{
			if (item == null)
			{
				item = new UserConfiguration()
				{
					StartPageId = Data.PageType.Rename,
				};
			}
			if (item.UserConfigurationId == null)
				item.UserConfigurationId = 1;
			if (string.IsNullOrWhiteSpace(item.Name))
				item.Name = "Default";
			item.CleanConfigContext = Sidvall.Serialization.SerializerManager.Current.SerializeObject(CleanConfig.Deserialize(item.CleanConfigContext));
			item.RenameConfigContext = Sidvall.Serialization.SerializerManager.Current.SerializeObject(RenameConfig.Deserialize(item.RenameConfigContext));
			item.SearchConfigContext = Sidvall.Serialization.SerializerManager.Current.SerializeObject(SearchConfig.Deserialize(item.SearchConfigContext));

			return item;
		}

		#endregion
		#region GetValidVersion

		private static string GetValidVersion(string version)
		{
			if (string.IsNullOrWhiteSpace(version))
				return ParameterManager.CurrentVersion;
			return version;
		}

		#endregion

		#endregion
	}
}